package com.minimalist.tc.service;

import com.minimalist.common.utils.ScheduleTaskService;
import com.minimalist.tc.manager.TcManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 车牌信息任务服务
 *
 * @author 11987
 */
@Service
public class TcPlateNumberTaskService{
    private static final Logger log = LoggerFactory.getLogger(TcPlateNumberTaskService.class);


    private static final String TASK_ID = "plateNumberTask";
    private static final String TASK_NAME = "车牌信息处理";

    @Autowired
    private TcManager tcManager;

    @Autowired
    private ScheduleTaskService scheduleTaskService;

    @Autowired
    private BaiduApiService baiduApiService;

    @Autowired
    private TcPlateNumberService tcPlateNumberService;

    /**
     * 启动车牌信息处理任务
     *
     * @param count           目标处理数量
     * @param intervalSeconds 间隔秒数
     * @return 是否成功启动
     */
    public boolean startPlateNumberTask(int count, int intervalSeconds){
        return scheduleTaskService.startTask(
                TASK_ID,
                TASK_NAME,
                count,
                intervalSeconds,
                () -> tcManager.insertPlateNumbersFromOrder(1));
    }

    /**
     * 启动持续处理车牌信息任务（不设置目标数量限制，排除没有文件ID的车牌）
     *
     * @param batchSize       每次处理数量
     * @param intervalSeconds 间隔秒数
     * @return 是否成功启动
     */
    public boolean startContinuousPlateNumberTask(int batchSize, int intervalSeconds){
        // 使用一个非常大的数字作为目标数量，实际上相当于无限运行
        final int INFINITE_COUNT = Integer.MAX_VALUE;

        return scheduleTaskService.startTask(
                TASK_ID,
                TASK_NAME,
                INFINITE_COUNT,
                intervalSeconds,
                () -> tcPlateNumberService.recognizePlateNumberWithFileId(batchSize));
    }

    /**
     * 停止车牌信息处理任务
     *
     * @return 是否成功停止
     */
    public boolean stopPlateNumberTask(){
        return scheduleTaskService.stopTask(TASK_ID);
    }

    /**
     * 获取车牌信息处理任务状态
     *
     * @return 任务状态
     */
    public ScheduleTaskService.TaskStatus getPlateNumberTaskStatus(){
        return scheduleTaskService.getTaskStatus(TASK_ID);
    }
}
